/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 100;

struct Lamp {
  int a, b;
} lamp[maxn + 1];

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> lamp[i].a >> lamp[i].b;
    }
    std::sort(lamp + 1, lamp + n + 1, [](const Lamp& lhs, const Lamp& rhs) {
      return (lhs.a != rhs.a ? lhs.a < rhs.a : lhs.b > rhs.b);
    });

    lamp[n + 1] = {0, 0};
    int cur = 0;
    long long ans = 0;
    std::queue<Lamp> q;

    for (int i = 1; i <= n; ++i) {
      while (!q.empty() && cur >= q.front().a) {
        q.pop();
      }
      cur++;
      ans += lamp[i].b;
      q.push(lamp[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("B. Lamps.in", "r", stdin);
  freopen("B. Lamps.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
